package com.seaside.park.api;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.seaside.common.bean.Result;
import com.seaside.common.util.FormatUtil;
import com.seaside.park.bean.Navi;
import com.seaside.park.mapper.NaviMapper;
import com.seaside.parkCommon.mapper.ParkPlaceMapper;
import com.seaside.parkCommon.vo.ParkPlaceVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags="导航")
@RestController
@RequestMapping("/api/navi")
public class NaviAPI {

    @Autowired
    private NaviMapper naviMapper;
    
    @Autowired
    private ParkPlaceMapper parkPlaceMapper;
    
    @ApiOperation(value = "根据车位号码发送导航图片", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="parkZoneId",dataTypeClass=String.class,required=true,value="车场代码"),
            @ApiImplicitParam(paramType="query",name="berthNumber",dataTypeClass=String.class,required=true,value="车位编号")
    })
    @PostMapping("/img")
    public Result<List<String>> getNaviImageList(@RequestParam String parkZoneId, @RequestParam String berthNumber) {
    	
    	List<String> imgList = new ArrayList<>();
    	
        Navi navi = naviMapper.getNaviInfo(parkZoneId, berthNumber);
        if (navi != null) {
            if(null != navi.getImgUrl1()) {
                imgList.add(FormatUtil.StringNullToEmpty(navi.getImgUrl1()));
            }
            if(null != navi.getImgUrl2()) {
                imgList.add(FormatUtil.StringNullToEmpty(navi.getImgUrl2()));
            }
            if(null != navi.getImgUrl3()) {
                imgList.add(FormatUtil.StringNullToEmpty(navi.getImgUrl3()));
            }
            if(null != navi.getImgUrl4()) {
                imgList.add(FormatUtil.StringNullToEmpty(navi.getImgUrl4()));
            }
            if(null != navi.getImgUrl5()) {
                imgList.add(FormatUtil.StringNullToEmpty(navi.getImgUrl5()));
            }
            if(null != navi.getImgUrl6()) {
                imgList.add(FormatUtil.StringNullToEmpty(navi.getImgUrl6()));
            }
        }
        
        Result<List<String>> result =  Result.OK("查询成功");
        result.setData(imgList);
        return result;
    }
    
    @ApiOperation(value = "根据车牌号查询车位信息", response = Result.class)
    @ApiImplicitParams({
    	@ApiImplicitParam(paramType="query",name="carPlate",dataTypeClass=String.class,required=true,value="车牌号")
    })
    @PostMapping("/info")
    public Result<ParkPlaceVO> getParkPlaceInfo(@RequestParam String carPlate) {
    	
    	if (StringUtils.isBlank(carPlate)) {
    		return Result.Error("请输入车牌号！");
    	}
    	
    	ParkPlaceVO vo = parkPlaceMapper.getParkPlaceByCurrPlate(carPlate);
    	if (vo != null) {
    		return Result.OK("查询成功", vo);
    	}

		return Result.Error("抱歉：未找到该车辆的停车位置！");
    }
    
    @ApiOperation(value = "保存导航信息", response = Result.class)
    @PostMapping("/save")
    public Result<Navi> saveNavi(@RequestBody Navi navi) {
    	
    	if (navi == null) {
    		return Result.Error("参数错误！");
    	}
    	
    	Navi origin = naviMapper.selectById(navi.getId());
    	if (origin == null) {
    		naviMapper.insert(navi);

    		return Result.OK("保存成功！", navi);
    	} else {
    		origin.setParkId(navi.getParkId());
    		origin.setImgUrl1(navi.getImgUrl1());
    		origin.setImgUrl2(navi.getImgUrl2());
    		origin.setImgUrl3(navi.getImgUrl3());
    		origin.setImgUrl4(navi.getImgUrl4());
    		origin.setImgUrl5(navi.getImgUrl5());
    		origin.setImgUrl6(navi.getImgUrl6());
    		
    		naviMapper.updateById(origin);
    		return Result.OK("保存成功！", origin);
    	}
    }
}
